/*
 * @FileName: [ConfigureServiceImpl.java]
 * 
 * @Package com.bcinfo.upp.sys.service.impl
 * 
 * 
 * Copyright (c) 2011-2015 BCINFO Technology Limited Com. All rights reserved.
 * 
 * This software is the confidential and proprietary information of BCINFO Technology Limited Company ("Confidential Information"). You shall not disclose such Confidential Information and shall use it only in accordance with the terms of the contract agreement you entered into with RKY.
 * 
 * $Rev: 3540 $ $LastChangedDate: 2013-06-24 16:33:32 +0800 (Mon, 24 Jun 2013) $ $LastChangedBy: lengzq $
 * 
 * @category bcinfo-upp
 * 
 * @version 1.1
 * 
 * @author $Author: lengzq $
 * 
 * Change History:[Formatter: author date description] <br/> 1 2 3
 */

package com.bci.pwtz.service.impl;

import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.ConfigMapper;
import com.bci.pwtz.mysql.model.Config;
import com.bci.pwtz.service.ConfigService;


/**
 * 配置服务 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * 提供系统配置参数的查询，按KEY值查询等操作
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 * </PRE>
 * <p>
 */
@Service
public class ConfigServiceImpl implements ConfigService
{
    private static final Logger logger=Logger.getLogger(ConfigServiceImpl.class);
    private ConfigMapper configMapper;

    /**
     * @return the configMapper
     */
    public ConfigMapper getConfigMapper()
    {
        return configMapper;
    }

    /**
     * @param configMapper the configMapper to set
     */
    public void setConfigMapper(ConfigMapper configMapper)
    {
        this.configMapper = configMapper;
    }

    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param key
     * @return
     * @throws PwtzDatabaseException 
     * @see com.bcinfo.upp.sys.service.ConfigureService#getConfigsByKey(java.lang.String)
     */
    public String[] getConfigsByKey(String key) throws PwtzDatabaseException
    {
        
        String value=getConfigByKey(key);
        if(value!=null&&value.trim().length()>0){
            return value.split("[,\\s+]");
        }
        return null;
    }
    
    /* (non-Javadoc)
     * Description: <br/> 
     *
     * @param key
     * @return
     * @throws PwtzDatabaseException 
     * @see com.bcinfo.upp.sys.service.ConfigureService#getConfigByKey(java.lang.String)
     */
    public String getConfigByKey(String key) throws PwtzDatabaseException
    {
        String value=null;
        try
        {
            List<Config> list = configMapper.selectByKey(key);
            if (list != null && list.size() > 0)
            {
                value=list.get(0).getConfigValue();
            }
        }
        catch (DataAccessException e)
        {
            throw new PwtzDatabaseException();
        }
        return value;
    }

    @Override
    public List<Config> selectConfigList() throws PwtzDatabaseException
    {
        List<Config> list=null;
        try{
            list=configMapper.selectConfigList();
        }catch(DataAccessException e){
            logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
         if (list!=null)
        {
            logger.debug("findSensitiveWords:result list=" + list.toString());
        }
        return list;
    }

	@Override
	public int updateLastSyncTime(String lastSyncTime)
			throws PwtzDatabaseException {
		int result=0;
		try {
			result=configMapper.updateLastSyncTime(lastSyncTime);
		} catch (DataAccessException e) {
			logger.error(ExceptionUtils.getStackTrace(e));
			throw new PwtzDatabaseException();
		}
		return result;
	}
}
